package com.mondo.financialwallet.presentation.ui.window;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.mondo.financialwallet.R;

import java.util.List;

/**
 * 类型选择popup window
 */
public class TypeSelectionPopupWindow {

    private LinearLayout mRootView;
    private CompatPopupWindow mPopupWindow;
    private OnClickListener onClickListener;

    public TypeSelectionPopupWindow(Context context, List<Type> typeList) {

        mRootView = new RadioGroup(context);
        mRootView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        mRootView.setBackgroundColor(Color.parseColor("#50000000"));
        mRootView.setOrientation(LinearLayout.VERTICAL);

        for (int i = 0; i < typeList.size(); i++) {
            Type type = typeList.get(i);
            RadioButton button = createText(type, context);
            mRootView.addView(button);
            if (i == 0){ button.setChecked(true); }
            if (i < typeList.size() - 1) {
                mRootView.addView(createDivider(context));
            }
        }

        initPopupWindow();
    }

    private View createDivider(Context context) {
        View divider = new View(context);
        divider.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                context.getResources().getDimensionPixelOffset(R.dimen.divider_size)));
        divider.setBackgroundColor(Color.parseColor("#D8D8D8"));
        return divider;
    }

    private RadioButton createText(Type type, Context context) {
        RadioButton text = new RadioButton(context);
        text.setBackgroundColor(Color.WHITE);
        text.setButtonDrawable(null);
        text.setTextColor(createColorStateList(
                Color.parseColor("#666666"),
                Color.parseColor("#3F82DD")));
        text.setText(type.getText());
        text.setTextSize(16);
        int padding = context.getResources().getDimensionPixelSize(R.dimen.default_view_space);
        text.setPadding(padding, padding, padding, padding);
        text.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));

        text.setOnClickListener(v -> {
            if(onClickListener != null) {
                onClickListener.onClick(type);
            }

            if (mPopupWindow.isShowing()){
                mPopupWindow.dismiss();
            }
        });
        return text;
    }

    private ColorStateList createColorStateList(int normal, int checked) {
        int[] colors = new int[]{checked, normal, checked, normal};
        int[][] states = new int[2][];
        states[0] = new int[]{android.R.attr.state_checked, android.R.attr.state_checked};
        states[1] = new int[]{};
        return new ColorStateList(states, colors);
    }

    private void initPopupWindow() {
        mPopupWindow = new CompatPopupWindow(mRootView,
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, true);
        mPopupWindow.setAnimationStyle(R.style.animQuotationPopupWindow);
        mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        mRootView.setOnClickListener(v -> {
            if (mPopupWindow.isShowing()) {
                mPopupWindow.dismiss();
            }
        });
    }

    public void show(View anchor) {
        mPopupWindow.showAsDropDown(anchor);
    }

    public void setOnClickListener(OnClickListener onClickListener){
        this.onClickListener = onClickListener;
    }

    public interface OnClickListener {
        void onClick(Type type);
    }

    public static class Type {
        private String id;
        private String text;

        public Type(String id, String text) {
            this.id = id;
            this.text = text;
        }

        public String getId() {
            return id;
        }

        public String getText() {
            return text;
        }
    }

}
